global replica_dir=	"<add path to your working directory>"

global data_dir=	"$replica_dir/data"
global output_dir=	"$replica_dir/output"

use "$data_dir/panel_simce4b6_sned_studlvl.dta", clear


/*----------------------------------------------------------------------------*/

**# Table A.4: Baseline checks for grade 6 and grade 8 cohorts, separately

/*----------------------------------------------------------------------------*/


global indepvars "mate_pre lect_pre nat_pre sch_municip_pre rural_rbd sch_stno_pre tipo_est_num_1 tipo_est_num_2 mom_edu_pre dad_edu_pre pad_inc_pre sch_gse_pre "  


collapse 	(mean) mate_pre lect_pre nat_pre mom_edu_pre dad_edu_pre pad_inc_pre /// 
			(firstnm) treat25 st_ind25 st_ind25_sq inter25 inter25_sq /// 
			(firstnm) treat10 st_ind10 st_ind10_sq inter10 inter10_sq /// 
			(firstnm) sch_municip_pre rural_rbd sch_stno_pre tipo_est_num_1 tipo_est_num_2 sch_gse_pre hg ///
			, by(rbd sned_yr)

			
la var st_ind25 "100% vs. 60%"
la var st_ind10 "60% vs. none"
 
la var mate_pre 	"Grade 4 Math Score"
la var lect_pre 	"Grade 4 Reading Score"
la var nat_pre 		"Grade 4 Natural Sciences Score"

la var sch_municip_pre 	"Municipal school"
la var rural_rbd 		"Rural location" 
la var sch_stno_pre 	"School enrolment"
la var tipo_est_num_1 	"School type, primary, grades 1-8"
la var tipo_est_num_2 	"School type, primary and secondary, grades 1-12"

la var mom_edu_pre 		"Education years, mother"
la var dad_edu_pre 		"Education years, father"
la var pad_inc_pre 		"Parental income, intervals"

la var sch_gse_pre 	"Socio-economic status"



save "$data_dir/temp.dta", replace

/*----------------------------------------------------------------------------*/


foreach i in 10 25{
	
use "$data_dir/temp.dta", clear

global indepvars "mate_pre lect_pre nat_pre sch_municip_pre rural_rbd sch_stno_pre tipo_est_num_1 tipo_est_num_2 mom_edu_pre dad_edu_pre pad_inc_pre sch_gse_pre "  

matrix define R = J(12, 4, .) // (#indepvars, #stats, missing values for all entries)

local label_st_ind`i': variable label st_ind`i'

	local k = 1
	foreach y of global indepvars {
		
		local label_`k': variable label `y'
		
		reg `y' treat`i' st_ind`i' st_ind`i'_sq inter`i' inter`i'_sq i.hg i.sned_yr if abs(st_ind`i') <= 1.5, vce(cluster rbd)
		
		matrix R[`k', 1] = r(table)[1,1] // rd estimate  
		matrix R[`k', 2] = r(table)[2,1] //se_ of rd estimate  
		matrix R[`k', 3] = r(table)[4,1] //p-value
		matrix R[`k', 4] = e(N) //N
		
		matrix list R
		local k = `k' + 1
		}

	matrix list R

preserve
	clear
	local t = `k' - 1
	svmat R
	gen R0 = ""
	forvalues k = 1 / `t' {
	replace R0 = "`label_`k''" if _n == `k'
	}
	order R0
	save "$data_dir/pre_results_`i'.dta", replace
restore

//search   dataout and install
use "$data_dir/pre_results_`i'.dta", clear

rename (R0 R1 R2 R3 R4) (Variable CoefficientEstimate StandardError pvalue Observations)

cd "$output_dir"
dataout, save(pre_6b4_`i'.tex) tex dec(3) replace

erase "$data_dir/pre_results_`i'.dta"

} 

erase "$data_dir/temp.dta"

*----------------------------------   end		
		
